package bulletGraph
{
	import mx.core.UIComponent;
	//import mx.core.UITextField;
	import mx.containers.VBox;
	import mx.core.EdgeMetrics;
	import flash.display.Sprite;
	
	
	
	[Style(name="titleStyleName", type="String")]
	public class BulletGraph extends UIComponent
	{
		private static var defaultStyles:Object = 
		{
			margin:10,
			fillColor: 0x92d050,
			darkness: [0.40, 0.25, 0.10],
			comparativeFillColor:0x00b050,
			performanceFillColor:0x0070c0,

			majorUnit:50,
			majorTickColor:0x7030a0,
			majorTickThickness:1,
			majorTickLength:6
		};
		
		private static var myClassName:String = "BulletGraph";
		include "../charts/core/defaultStyles.as";
		
		private var _background:Sprite;
		private var _titleBox:VBox;
		private var _title:UITextField;
		private var _subtitle:UITextField;
		
		private var _plotArea:BulletGraphPlotArea;
		
		private var _showTitles:Boolean = true;
		private var _titleText:String = "Bullet Graph";
		private var _subtitleText:String = "Sample Sub Title";
		
		private var _titlesChanged:Boolean = false;
		
		private var _objectSpacing:Number = 8;
		protected var chromeMetrics:EdgeMetrics = new EdgeMetrics(); 
 
    //-- Title Properties
		[Inspectable(defaultValue="true")]
		public function get showTitles():Boolean
		{
			return this._showTitles;
		}
		
		public function set showTitles(show:Boolean):void
		{
			if(this._showTitles != show)
			{
				this._showTitles = show;
				this._titlesChanged = true;
				this.invalidateProperties();
			}
		}
		
		/**
		 *  The text that appears in the chart's title label.
		 */
		public function get title():String
		{
			return this._titleText;
		}
		
		/**
		 *  @private
		 */
		public function set title(value:String):void
		{
			if(value == null) value = "";
			
			if(this._titleText != value)
			{
				this._titleText = value;
				this._titlesChanged = true;
				this.invalidateProperties();
			}
		}
		
		/**
		 *  The text that appears in the chart's subtitle label.
		 */
		public function get subtitle():String
		{
			return this._subtitleText;
		}
		
		/**
		 *  @private
		 */
		public function set subtitle(value:String):void
		{
			if(value == null) value = "";
			
			if(this._subtitleText != value)
			{
				this._subtitleText = value;
				this._titlesChanged = true;
				this.invalidateProperties();
			}
		}
		
			
		
		override protected function createChildren():void
		{
			super.createChildren();
			
			if(!_background)
			{
				_background = new Sprite();
				addChild(_background);
			}
			if(!_titleBox)
			{
				_titleBox = new VBox();
				_titleBox.styleName = this.getStyle("titleBoxStyleName");
				addChild(_titleBox);
			}
			if(!_title)
			{
				_title = new UITextField();
				_titleBox.addChild(_title);
				_title.text = _titleText;
				_title.styleName = this.getStyle("titleStyleName");
				this._title.selectable = false;
				this._title.multiline = true;
				this._title.wordWrap = true;
				this._title.percentWidth = 100;
			}
			if(!_subtitle)
			{
				_subtitle = new UITextField();
				_titleBox.addChild(_subtitle);
				_subtitle.text = _subtitleText;
				_subtitle.styleName = this.getStyle("subTitleStyleName");
				this._subtitle.selectable = false;
				this._subtitle.multiline = true;
				this._subtitle.wordWrap = true;
				this._subtitle.percentWidth = 100;
			}
			if(!_plotArea)
			{
				_plotArea = new BulletGraphPlotArea();
				addChild(_plotArea);
			}
		}
		
		/**
		 *  Sets the default sizes.
		 */
		override protected function measure():void
		{
			super.measure();
			
			this.measuredMinWidth = 100;
			this.measuredMinHeight = 20;
			
			//Just use the defaults. The user will set explicit values.
			this.measuredWidth = 370;
			this.measuredHeight = 80;
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			
			// background
			if(_background)
			{
				_background.graphics.clear();
				_background.graphics.lineStyle(1,0xad1f30,1.0);
				_background.graphics.drawRect(0,0,unscaledWidth,unscaledHeight);
			}
			
			var margin:Number = this.getStyle("margin");
			this._titleBox.visible = this._showTitles && (this._titleText || this._subtitleText);
			
			// title & sub title
			if(this._titleBox.visible)
			{
				this._titleBox.x = margin;
				this._titleBox.y = margin;
				
				var titleWidth:Number = unscaledWidth/3 - margin * 2;
				
				var borderWidth:Number = this._titleBox.getStyle("borderThickness") * 2;
				if (isNaN(borderWidth)) borderWidth = 0;
				
				this._title.setActualSize(titleWidth - borderWidth, this._title.getExplicitOrMeasuredHeight());
                this._subtitle.setActualSize(titleWidth - borderWidth, this._subtitle.getExplicitOrMeasuredHeight());

				this._titleBox.width = titleWidth;
				this._titleBox.invalidateSize();
				this._titleBox.validateSize(true);
				this._titleBox.setActualSize(this._titleBox.getExplicitOrMeasuredWidth(), this._titleBox.getExplicitOrMeasuredHeight());

				//update the chrome metrics so that the plot area will appear at the correct position.
				this.chromeMetrics.left += this._titleBox.getExplicitOrMeasuredWidth() + _objectSpacing;
			}
			
			_titleBox.move(-100,-50);
			
			
			// plot area
			this._plotArea.move(Math.round(this.chromeMetrics.left), Math.round(this.chromeMetrics.top));
			this._plotArea.width = unscaledWidth - (this.chromeMetrics.left + this.chromeMetrics.right);
			this._plotArea.width = Math.max(this._plotArea.width, this._plotArea.minWidth);
			this._plotArea.height = unscaledHeight;
			this._plotArea.height = Math.max(this._plotArea.height, this._plotArea.minHeight);
			
			this.chromeMetrics = new EdgeMetrics();
		}
		
		override protected function commitProperties():void
		{
			super.commitProperties();
			
			
			if(this._titlesChanged)
			{
				this._title.text = _titleText;
				this._subtitle.text = _subtitleText;
				
				this.invalidateDisplayList(); // invalidate in case the titles require more room			
				this._titlesChanged = false;
			}
		}
		
	
	}
}


import mx.core.UITextField;

class UITextField extends mx.core.UITextField
{
	
}